#include "map.h"
#include <QFileInfo>

Map::Map()
{
    distance = nullptr;
}

int Map::Read(QString path)
{
    cityList.clear();
    QFileInfo fi(path);
    if(!fi.isFile()) return 0;
    QFile mapFile(path);
    if(mapFile.open(QFile::ReadOnly)){
        QTextStream in(&mapFile);
        int row = 0;
        while (!in.atEnd()) {
            QString     line    = in.readLine();
            QStringList cityInfo = line.split(",");
            cityList.append(new City(cityInfo));
        }
    }
    int n = cityList.count();

    distance = new int*[n];
    for (int i = 0; i < n; ++i) {
        distance[i]=new int[n];
        for (int j = 0; j < n; ++j) {
            distance[i][j]=(*cityList.at(i)-*cityList.at(j));
        }
    }
    return n;
}

City* Map::getCity(QString name)
{
    foreach (City* city, cityList) {
        if(city->name()==name) return city;
    }
    return nullptr;
}

QDebug operator<<(QDebug debug,Map &m){
    foreach (auto city, m.cityList) {
        debug<<city->name()<<" 经度："<<city->lon()<<" 纬度："<<city->lan()<<Qt::endl;
    }
    return debug;
}
